package web

import (
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

/*
todo
	录实验
		实验物品++
			生成实验物品关系表数据
	删实验
		删除实验信息、实验物品关系数据
	做实验
		选择实验物品
			实验物品id查找
				1、事故物品关系表
					需要学习的模块
						查询学习记录表
							查找缺少学习记录||未通过模块对应测试题目
								生成测试题目
									完成学习内容
				2、物品表(药品、设备、耗材)
					所有相关模块
						生成测试题目
							完成学习内容
*/

type ExperimentHandler struct {
	svc *service.ExperimentService
}

func NewExperimentHandler(svc *service.ExperimentService) *ExperimentHandler {
	return &ExperimentHandler{
		svc: svc,
	}
}

func (a *ExperimentHandler) RegistryRoutes(server *gin.Engine) {
	ag := server.Group("/experiment")
	ag.POST("/query", a.Query)
	ag.POST("/create", a.Create)
	ag.POST("/delete", a.Delete)
	ag.POST("/module-test", a.QueryModuleTest)
	ag.POST("/related-module", a.QueryRelatedModule)
	ag.POST("/need-to-learn-module", a.QueryNeedToLearnModule)
}

func (a *ExperimentHandler) Query(ctx *gin.Context) {
	type QueryReq struct {
		Name string `json:"experiment_name"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	Experiment, err := a.svc.Query(ctx, domain.Experiment{
		Name: req.Name,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, Experiment)
}

func (a *ExperimentHandler) Delete(ctx *gin.Context) {
	type QueryReq struct {
		Ids []string `json:"ids"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	affectedRows, err := a.svc.Delete(ctx, req.Ids)

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"rows": affectedRows,
	})
}

func (a *ExperimentHandler) Create(ctx *gin.Context) {
	type QueryReq struct {
		Name          string   `json:"name"`
		NameEn        string   `json:"name_en"`
		Chemicals     []string `json:"chemicals"`
		Equipments    []string `json:"equipments"`
		Consumables   []string `json:"consumables"`
		IfEnabled     int      `json:"if_enabled"`
		Description   string   `json:"description"`
		DescriptionEn string   `json:"description_en"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	createdRows, err := a.svc.Create(ctx, domain.Experiment{
		Name:          req.Name,
		NameEn:        req.NameEn,
		Chemicals:     req.Chemicals,
		Equipments:    req.Equipments,
		Consumables:   req.Consumables,
		IfEnabled:     req.IfEnabled,
		Description:   req.Description,
		DescriptionEn: req.DescriptionEn,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"rows": createdRows,
	})
}

func (a *ExperimentHandler) QueryModuleTest(ctx *gin.Context) {
	type QueryReq struct {
		ModuleId string `json:"module_id"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	Experiment, err := a.svc.QueryModuleTest(ctx, req.ModuleId)

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, Experiment)
}

func (a *ExperimentHandler) QueryRelatedModule(ctx *gin.Context) {
	type QueryReq struct {
		Id string `json:"id"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	CommonItems, err := a.svc.QueryRelatedModule(ctx, domain.Experiment{
		Id: req.Id,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, CommonItems)
}

func (a *ExperimentHandler) QueryNeedToLearnModule(ctx *gin.Context) {
	type QueryReq struct {
		Id string `json:"id"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	CommonItems, err := a.svc.QueryNeedToLearnModule(ctx, domain.Experiment{
		Id: req.Id,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, CommonItems)
}
